package com.study.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.study.common.DateUtils;
import com.study.model.pojo.ReciprocatingUnit;
import com.study.services.ReciprocatingUnitServices;

@RestController
public class ReciprocatingUnitController {
	@Autowired
	ReciprocatingUnitServices rus;

	@GetMapping("info/{keywords}")
	public JSONArray getInfoByKewords(@PathVariable(required = false) String keywords) {
		System.out.println("info++");
		JSONArray json = new JSONArray();
		List<ReciprocatingUnit> list = rus.selectAllByKeywords(keywords);
		for (ReciprocatingUnit reciprocatingUnit : list) {
			JSONObject jObject = new JSONObject();
			jObject.put("recId", reciprocatingUnit.getRecId());
			jObject.put("contactPhone", reciprocatingUnit.getContactPhone());
			jObject.put("eid", (Integer) reciprocatingUnit.getEid());
			jObject.put("recAddress", reciprocatingUnit.getRecAddress());
			jObject.put("recClass", reciprocatingUnit.getRecClass());
			jObject.put("recContact", reciprocatingUnit.getRecContact());
			jObject.put("recCreationData", DateUtils.dateFormat(reciprocatingUnit.getRecCreationData()));
			jObject.put("recName", reciprocatingUnit.getRecName());
			jObject.put("recPhone", reciprocatingUnit.getRecPhone());
			jObject.put("recRegistrationData", DateUtils.dateFormat(reciprocatingUnit.getRecRegistrationData()));
			jObject.put("recState", reciprocatingUnit.getRecState());

			json.add(jObject);
			System.out.println(reciprocatingUnit);
		}
		return json;
	}

	@RequestMapping("/info")
	public JSONArray info() {
		System.out.println("info");
		JSONArray json = new JSONArray();
		List<ReciprocatingUnit> reciprocatingUnits = rus.selectAll();
		for (ReciprocatingUnit reciprocatingUnit : reciprocatingUnits) {
			JSONObject jObject = new JSONObject();
			jObject.put("recId", reciprocatingUnit.getRecId());
			jObject.put("contactPhone", reciprocatingUnit.getContactPhone());
			jObject.put("eid", (Integer) reciprocatingUnit.getEid());
			jObject.put("recAddress", reciprocatingUnit.getRecAddress());
			jObject.put("recClass", reciprocatingUnit.getRecClass());
			jObject.put("recContact", reciprocatingUnit.getRecContact());
			jObject.put("recCreationData", DateUtils.dateFormat(reciprocatingUnit.getRecCreationData()));
			jObject.put("recName", reciprocatingUnit.getRecName());
			jObject.put("recPhone", reciprocatingUnit.getRecPhone());
			jObject.put("recRegistrationData", DateUtils.dateFormat(reciprocatingUnit.getRecRegistrationData()));
			jObject.put("recState", reciprocatingUnit.getRecState());

			json.add(jObject);
		}
		return json;
	}

	@RequestMapping("/infoById")
	public JSONObject infoById(String recId) {
		System.out.println("infoById");
		JSONObject jObject = new JSONObject();
		ReciprocatingUnit reciprocatingUnit = rus.selectById(recId);
		jObject.put("infoById", reciprocatingUnit);
		return jObject;
	}

//	@Mapping("/addRec")
	@RequestMapping("/addRec")
	public JSONObject addRec(ReciprocatingUnit reciprocatingUnit) {
		System.out.println("addRec");
		System.out.println(reciprocatingUnit);
		JSONObject jObject = new JSONObject();
		int insertRec = rus.insertRec(reciprocatingUnit);
		jObject.put("insertRec", insertRec);
		return jObject;
	}

	@RequestMapping(value="/updateRec")
	public JSONObject updateRec(ReciprocatingUnit reciprocatingUnit) {
		System.out.println("updateRec");
		JSONObject jObject = new JSONObject();
		int updataRec = rus.updataRec(reciprocatingUnit);
		jObject.put("updateRec", updataRec);
		return jObject;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
}
